package control;

import java.util.ArrayList;
import java.util.Date;

import model.*;
import db.*;


public class CtrRent {
	
	private Rent rent;
	private DBRent dbRent;
	private DBReport reDb;
	private CtrRenter ctrRenter;
	private CtrApartment ctrApartment;
	private CtrReservation ctrReservation;
	private CtrInstallation iCtr;
	private DBReportEvaluation repevDb;

	public CtrRent() {
		this.rent = new Rent();
		this.dbRent = new DBRent();
		this.ctrRenter = new CtrRenter();
		this.ctrApartment = new CtrApartment();
		this.ctrReservation = new CtrReservation();
		this.iCtr = new CtrInstallation();
		this.reDb = new DBReport();
		this.repevDb = new DBReportEvaluation();

	}
	
	public void moveIn(int renterId, int apartmentId){
		Renter r = ctrRenter.getRenter(renterId); 
		Apartment a = ctrApartment.getApartment(apartmentId);
		
		this.ctrApartment.setRentedStatus(a);
		this.ctrRenter.setMoveInStatus(r);
		this.ctrReservation.removeReservation(renterId);
		
		this.rent.setMoveInDate(new Date());
		this.rent.setApartment(a);
		this.rent.setRenter(r);
		this.rent.setId(this.dbRent.insertScope(rent));
		
	}
	public ArrayList<Installation> startMoveOut(int apartmentId)
	{
		this.rent = dbRent.getActiveByApartmentId(apartmentId);
		
		return this.iCtr.getByApartmentId(apartmentId);
		
		//get installations by apartment id
	}
	
	public void finishMoveOut()
	{
		ctrRenter.setMoveOutStatus(ctrRenter.getRenter(rent.getRenter().getId()));
		ctrApartment.setEmptyStatus(ctrApartment.getApartment(rent.getApartment().getId()));
		rent.setMoveOutDate(new Date());
		
		System.out.println(rent.getId());
		System.out.println(rent.getApartment().getId());
		System.out.println(rent.getMoveInDate());
		System.out.println(rent.getMoveOutDate());
		System.out.println(rent.getRenter().getId());
		
		dbRent.update(rent);
	}
	public ArrayList<ReportEvaluation> getReports()
	{
		//get all installations for apartment and reports for them by rent
		ArrayList<ReportEvaluation> repev = repevDb.getByApartmentId(rent.getApartment().getId(),rent.getId());
		//System.out.println(repev);
		return repev;

	}
	public ArrayList<Reservation> getWaitingList()
	{
		//get approved reservations by apartment and apartment type
		//System.out.println(rent.getApartment().getId());
		int apartmentId = rent.getApartment().getId();
		ArrayList<Reservation> apartments = ctrReservation.getByApartment(apartmentId);
		//System.out.println(rent.getApartment().getType().getId());
		int typeId = ctrApartment.getApartment(apartmentId).getType().getId();
		ArrayList<Reservation> types = ctrReservation.getByApartmentType(typeId);
		ArrayList<Reservation> combined = new ArrayList<Reservation>();
		combined.addAll(apartments);
		combined.addAll(types);
		return combined;
	}
	public void addReport(int installationId, String condition, ReportType rt)
	{
		System.out.println("adding report");
		Installation i = this.iCtr.buildInstallation(installationId);
		Report r = new Report();
		r.setInstallation(i);
		r.setType(rt);
		r.setRent(this.rent);
		System.out.println("Rent ID:"+rent.getId());
		r.setCondition(condition);
		r.setId(this.reDb.insertScope(r));
	}
	
//	public void moveOut(int apartmentId){
//		Rent re = dbRent.getByApartment(apartmentId);
//		Renter r=this.ctrRenter.getRenter(re.getApartment().getId());
//		Apartment a = ctrApartment.getApartment(apartmentId);
//		
//		this.ctrApartment.setRentedStatus(a);
//		this.ctrRenter.setMoveOutStatus(r);
//		this.dbRent.update(rent);
//	}
/*	
	public ArrayList<Renter> getWLByappartment(int apartmentId){
		ArrayList<Renter> waitingList = new ArrayList<Renter>();
		waitingList = this.ctrReservation.getByApartment(apartmentId);
	
		return waitingList;
	}
	public ArrayList<Renter> getWLByApType(int apartmentTypeId){
		ArrayList<Renter> waitingList = new ArrayList<Renter>();
		waitingList = this.ctrReservation.getByApartmentType(apartmentTypeId);
	
		return waitingList;
	}
*/
}
